package com.pearl.seata.demo.controller;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pearl.seata.demo.entity.OrderEntity;
import com.pearl.seata.demo.service.OrderService;
import io.seata.spring.annotation.GlobalTransactional;
import io.seata.tm.api.GlobalTransaction;
import io.seata.tm.api.GlobalTransactionContext;
import org.apache.shardingsphere.infra.hint.HintManager;
import org.apache.shardingsphere.transaction.api.TransactionType;
import org.apache.shardingsphere.transaction.base.seata.at.SeataTransactionHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.IntStream;

/**
 * <p>
 * 订单信息表 前端控制器
 * </p>
 *
 * @author 自动代码生成
 * @since 2024-01-24
 */
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @GetMapping("/update")
    @Transactional
    public Object update() {
        //SeataTransactionHolder.set(GlobalTransactionContext.getCurrent());
        // 更新: ds_order_0.t_order_8  1759750089594126338
        OrderEntity order1 = orderService.getById(1759750089594126338L);
        order1.setOrderTime(LocalDateTime.now());
        orderService.updateById(order1);


        // 更新: ds_order_1.t_order_1  1760191011712675841
        OrderEntity order2 = orderService.getById(1760191011712675841L);
        order2.setOrderTime(LocalDateTime.now());
        orderService.updateById(order2);

        // 调用第三方服务
/*        String response = HttpUtil.get("http://localhost:8087/order/update");
        System.out.println(response);*/

        if (true){
            throw new RuntimeException();
        }
        return "操作成功";
    }
}